//Copyright (c) 2009, Code Endeavors, LLC - 'http://www.codeendeavors.com
//This source is subject to the BSD License.
//See http://www.opensource.org/licenses/bsd-license.php
using System;
using System.Collections.Generic;
using System.Web.UI;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;

namespace $YourCompany$.Modules.$safeprojectname$
{
    [ControlMethodClass("$YourCompany$.Modules.$safeprojectname$.View$safeprojectname$")]
    partial class View$safeprojectname$ : AjaxPortalModuleBase, DotNetNuke.Entities.Modules.IActionable
    {

        //hookup events
        override protected void OnInit(EventArgs e)
        {
            this.AddScriptComponentDescriptors += new AddScriptComponentDescriptorsDelegate(this.AjaxPortalModuleBase_AddScriptComponentDescriptors);
            this.AddScriptReferences += new AddScriptReferencesDelegate(this.AjaxPortalModuleBase_AddScriptReferences);
            this.AddLocalizedMessages += new AddLocalizedMessagesDelegate(this.AjaxPortalModuleBase_AddLocalizedMessages);
            base.OnInit(e);
        }

#region AjaxPortalModuleBase Events
        //-------------------------------------------------------------------------------------------------------------
        //- This is where you add your custom properties that are to be sent down to the client-side object
        //- By default we are passing the naming container ID (ns) and localized messages (msgs) 
        //- If you add your own properties make sure you update your client code with those properties found in Edit$safeprojectname$.ascx.js
        //-      get_ImagePath: function() {return this._imagePath;},
        //-      set_ImagePath: function(value) {this._imagePath = value;},
        //-------------------------------------------------------------------------------------------------------------
        protected void AjaxPortalModuleBase_AddScriptComponentDescriptors(ScriptComponentDescriptor Descriptor)
        {
            //IMPORTANT!  Enter Client Namespace + ObjectName as Type
            Descriptor.Type = "$YourCompany$.View$safeprojectname$";

            //---------------------------------------------------------------------------------------------------------
            //Add custom properties here
            //   Descriptor.AddScriptProperty("ImagePath", String.Format("'{0}'", this.ModulePath + "images/"));
            //---------------------------------------------------------------------------------------------------------
        }

        //-------------------------------------------------------------------------------------------------------------
        //- This is where your client-side javascript that uses the MS AJAX framework needs to be registered
        //- Adding the reference here ensures that the MS AJAX script is run before our script which uses things like Type.registerNamespace run
        //-------------------------------------------------------------------------------------------------------------
        protected void AjaxPortalModuleBase_AddScriptReferences(List<ScriptReference> References)
        {
            References.Add(new ScriptReference(this.ModulePath + "View$safeprojectname$.ascx.js"));
        }

        //-------------------------------------------------------------------------------------------------------------
        //- Add any localized text needed on the client
        //-------------------------------------------------------------------------------------------------------------
        protected void AjaxPortalModuleBase_AddLocalizedMessages(Dictionary<string, string> Messages)
        {
            Messages["btnHello"] = Localization.GetString("btnHello.Client", LocalResourceFile);
        }

#endregion

#region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Settings.ContainsKey("Name"))
                    lblName.Text = Settings["Name"].ToString();
                
                if (Settings.ContainsKey("Desc"))
                    lblDesc.Text = Settings["Desc"].ToString();
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

#endregion

#region Control Methods
        //-------------------------------------------------------------------------------------------------------------
        //- Exposing methods to the client code is as simple as adding the ControlMethod attribute
        //- IMPORTANT!   ALWAYS SECURE YOUR CONTROL METHODS IF INTERACTING WITH SENSITIVE DATA
        //-------------------------------------------------------------------------------------------------------------
        [ControlMethod()]
        public string Hello(string Name, string Description)
        {
            return String.Format("Hello {0}, your description was submitted at {1}", Name, DateTime.Now.ToString());
        }
#endregion


#region Optional Interfaces

        // Registers the module actions required for interfacing with the portal framework
        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get 
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("Edit.Text", LocalResourceFile), ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

#endregion

    }

}
